package gr.takeda.rhythmguitar.main;

import gr.takeda.rhythmguitar.gui.RhythmGuitarLogic;
import gr.takeda.rhythmguitar.tools.LookAndFeelManager;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.apache.log4j.BasicConfigurator;

/**
 * Contains the main method. Is responsible for running the application, 
 * actually showin up the main frame of the application, which is a RhythmGuitarLogic.
 * The RhythmGuitarLogic is a BaseFrame, thus it contains an instance of JFrame which
 * can be accessed though the BaseFrame's getFrame() method. 
 * 
 * Beaware of the RhythmGuitarLogic.restartFrameOnExit property, this is a hack which 
 * solves the problem of restarting the frame (hiding and showing completely) without 
 * exiting the process.
 * 
 * This hack should be improved or completely removed when an alternative approach
 * is available.
 * 
 * 
 * @author takeda
 *
 */

public class Mein {

	public static final Object lock = new Object();
	public static volatile boolean locked = true;
	public static boolean enabled = true;
	public static RhythmGuitarLogic rhythmGuitar;

	public static void main(String[] tests) {
		BasicConfigurator.configure();
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);

		while (enabled) {
			LookAndFeelManager.updateUI();
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					rhythmGuitar = new RhythmGuitarLogic();
					rhythmGuitar.getFrame().setVisible(true);
				}
			});
			while (locked) {
				synchronized (lock) {
					try {
						lock.wait();
					} catch (InterruptedException e) {
					}
				}
			}
			enabled = rhythmGuitar.restartFrameOnExit;
			locked = true;
		}
	}
}
